package com.tianhao.controller;

import com.tianhao.entity.PoHeader;
import com.tianhao.export.util.KnifeListSplitUtil;
import com.tianhao.export.util.SimplePoiExportUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author tianhao.luo@hand-china.com
 * @version 1.0
 * @date 2022年05月12日 星期四 11:03 上午
 * @description
 * @since 1.0
 */
@RestController
@RequestMapping("/poHeader")
public class PoHeaderController {

    public List<PoHeader> initData(Integer size) {
        List<PoHeader> poHeaders = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            PoHeader poHeader = new PoHeader();
            poHeader.setPoNum("Order" + i % 2);
            poHeader.setLineNum(i % 3);
            poHeader.setProductNum(UUID.randomUUID().toString());
            poHeader.setProductName(UUID.randomUUID().toString());
            poHeader.setQuantity(new BigDecimal(i));
            poHeader.setUnit("");
            poHeader.setSo("");
            poHeader.setSoItem("");
            poHeader.setIcpo("");
            poHeader.setMarket("");
            poHeader.setDeliveryDate(new Date());
            poHeader.setItemNum("");
            poHeader.setItemName("");
            poHeader.setStanderQuantity(new BigDecimal(i));
            poHeader.setNeedQuantity(new BigDecimal(i));
            poHeader.setInkjetCodeRequirement("标签" + i);
            poHeaders.add(poHeader);
        }
        return poHeaders;
    }

    @GetMapping
    public void get(HttpServletResponse httpServletResponse){
        // 准备数据
        List<PoHeader> poHeaders = initData(100);
        List<PoHeader> testList = KnifeListSplitUtil.groupThenSortList(poHeaders, PoHeader::getPoNum, Comparator.comparing(String::toString), Comparator.comparing(PoHeader::getLineNum));
        SimplePoiExportUtil.export(httpServletResponse,testList,PoHeader.class,Comparator.comparing(PoHeader::getPoNum),PoHeader::getPoNum);
    }

}
